package com.shangcheng.dubbo.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.dubbo.mappers.CmsPrefrenceAreaProductRelationMapper;
import com.shangcheng.model.domain.CmsPrefrenceAreaProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class CmsPrefrenceAreaProductRelationApiImpl implements CmsPrefrenceAreaProductRelationApi {
    @Autowired
    private CmsPrefrenceAreaProductRelationMapper prefrenceAreaProductRelationMapper;

    @Override
    public void save(List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList, Long id) {
        if (ObjectUtil.isNotEmpty(prefrenceAreaProductRelationList)) {
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
                cmsPrefrenceAreaProductRelation.setProductId(id);
                prefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
            }
        }
    }

    @Override
    public List<CmsPrefrenceAreaProductRelation> findByProductId(Integer id) {
        return prefrenceAreaProductRelationMapper.selectList(new LambdaQueryWrapper<CmsPrefrenceAreaProductRelation>().eq(CmsPrefrenceAreaProductRelation::getProductId, id));
    }

    @Override
    public void updateById(Integer id, List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList) {
        prefrenceAreaProductRelationMapper.delete(new LambdaQueryWrapper<CmsPrefrenceAreaProductRelation>().eq(CmsPrefrenceAreaProductRelation::getProductId, id));
        this.save(prefrenceAreaProductRelationList, Convert.toLong(id));
    }
}
